package com.loop.unclelend.ui.activity

import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.loop.unclelend.R
import com.loop.unclelend.databinding.UiPageMessageBinding
import com.loop.unclelend.pro.ProActivity
import com.loop.unclelend.ui.adapter.Adapter_Message
import com.loop.unclelend.ui.presenter.Pre_Message
import com.loop.unclelend.ui.view.IView_Message
import com.loop.unclelend.utils.event.UncleEventData
import com.loop.unclelend.utils.event.UncleEventHelper
import com.loop.unclelend.utils.event.UncleEventType
import com.loop.unclelend.utils.helper.CustomProcessHelper
import com.loop.unclelend.utils.helper.TestDataLibInvoke
import org.json.JSONObject

/**
 * UncleFlow
 * @Author wwdeng
 * @CreateTime: 2024/8/19 17:05
 * @Annotation：消息列表
 */
class Act_Message: ProActivity(),IView_Message {
    lateinit var binding: UiPageMessageBinding
    private var presenter: Pre_Message? = null

    private var messageAdapter: Adapter_Message? = null

    override fun getUncleRootView(): View {
        binding = UiPageMessageBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initUncleView() {
        setUncleTitleBarVisibility(View.VISIBLE)
        setUncleBackBtnRes(R.mipmap.icon_back)
        setUncleTextTitle(getString(R.string.message_title))
        setUncleRightBtnVisibility(View.GONE)

        initUncleRefreshLayout()
        initUncleRecyclerView()
    }

    override fun initUncleListener() {
    }

    override fun initUncleData() {
        if (presenter == null){
            presenter = Pre_Message(this)
        }
        showUncleLoading()
        presenter!!.requestMessageListAction()
    }

    /* 初始化刷新 */
    private fun initUncleRefreshLayout() {
        //禁止加载
        binding.refreshLayout.setEnableLoadMore(false)
        binding.refreshLayout.setEnableAutoLoadMore(false)
        binding.refreshLayout.setOnRefreshListener {
            presenter!!.requestMessageListAction()
        }
        binding.refreshLayout.setOnLoadMoreListener {
            binding.refreshLayout.finishLoadMore()
        }
    }

    /**
     * 初始化消息列表
     */
    private fun initUncleRecyclerView() {
        binding.recyclerView.setHasFixedSize(true)
        binding.recyclerView.isNestedScrollingEnabled = false
        binding.recyclerView.isFocusableInTouchMode = false //不需要焦点
        binding.recyclerView.layoutManager = LinearLayoutManager(this)
        messageAdapter = Adapter_Message(binding.recyclerView)
        binding.recyclerView.adapter = messageAdapter!!
        binding.recyclerView.itemAnimator = DefaultItemAnimator()
        messageAdapter!!.setOnItemChildClickListener { _, childView, position ->
            val item = messageAdapter!!.getItem(position)
            when(item.optString("type")){
                "REPAY_REMIDER",//还款提醒
                "APPLY_STATUS_UPDATE" -> {//申请状态更新
                    //进入还款页
                    publishEventData(UncleEventData(UncleEventData.TYPE.UNCLE_SHOW_REPAY))
                }
                else -> {//AD_MESSAGE  广告推广
                    CustomProcessHelper.getUncleStepList()
                    if (CustomProcessHelper.getUncleStepList().size == 0) {//流程已完成 - 进入首页
                        //进入还款页
                        publishEventData(UncleEventData(UncleEventData.TYPE.UNCLE_SHOW_HOME))
                    } else {
                        //流程未完成 - 进入流程页面
                        //上报
                        TestDataLibInvoke.sycData()
                        onIntent(Act_Steps::class.java)
                        finish()
                    }
                }
            }
        }
    }

    /**
     * 设置消息列表
     */
    override fun showUncleMessageList(msgList: ArrayList<JSONObject>) {
        messageAdapter!!.data = msgList
    }

    override fun hideUncleLoading() {
        super.hideUncleLoading()
        binding.refreshLayout.finishRefresh()
    }

    override fun onStart() {
        super.onStart()
        UncleEventHelper.addUncleRequestEventAction(UncleEventType.PAGE.UNCLE_PAGE_MESSAGE_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        UncleEventHelper.addUncleRequestEventAction(UncleEventType.PAGE.UNCLE_PAGE_MESSAGE_END)
    }
}